/* 用户的增删改查 */
const router = require("koa-router")();
(DB = require("../../module/db.js")), (tools = require("../../module/tools"));

router.get("/", async ctx => {
  var result = await DB.find("articlecate", {});
  await ctx.render("admin/articlecate/index", {
    list: tools.cateToList(result)
  });
});
router.get("/add", async ctx => {
  //获取一级分类
  var result = await DB.find("articlecate", { pid: "0" });
  await ctx.render("admin/articlecate/add", {
    catelist: result
  });
});
router.post("/doAdd", async ctx => {
  var addData = ctx.request.body;
  console.log(addData)
  var result = await DB.insert("articlecate", addData);
  ctx.redirect(ctx.state.__HOST__ + "/admin/articlecate");
});

router.get("/edit", async ctx => {
  var id = ctx.query.id;
 
  var result = await DB.find("articlecate", { _id: DB.getObjectId(id) });
  
  var articlecate = await DB.find("articlecate", { pid: "0" });

  await ctx.render("admin/articlecate/edit", {
    list: result[0],
    catelist: articlecate
  });
});

router.post("/doEdit", async ctx => {
  var editData = ctx.request.body;
  var id = editData.id;
  var title = editData.title;
  var pid = editData.pid;
  var keywords = editData.keywords;
  var status = editData.status;
  var description = editData.description;
  var result = await DB.update(
    "articlecate",
    { '_id': DB.getObjectId(id) },
    {
      title,
      pid,
      keywords,
      status,
      description
    }
  );
  ctx.redirect(ctx.state.__HOST__ + "/admin/articlecate");
});
/* router.get('/del',async(ctx) =>{
    ctx.body = '用户删除'
}) */
module.exports = router.routes();
